$(document).ready(function() {
	var width = $(window).width() - 155 + 16;
	$("#right").css("width", width + "px");
	var height = $(window).height();
	$("#division_line_left_right").css("height", "1000px");
	InitLeftMenu();
	tabClose();
	tabCloseEven();
	$('#bottom_navigation').tabs('add', {
		title : "现有文章",
		content : createFrame("article_show.php"),
		closable : false,
	});
	$(window).resize(function() {
		resize();
	});
	$("#menu ul li a").each(function(index) {
		$(this).click(function() {
			var choice = $('#allowed_operator').accordion('getSelected');
			if (choice) {
				var title = choice.panel("options").title;
				$('#allowed_operator').accordion("remove", title);
			}
			var n = _menus.menus[index];
			var menulist = '';
			menulist += '<ul>';
			$.each(n.menus, function(j, o) {
				menulist += '<li><div><a  href="#" rel="' + o.url + '" ><span>&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div></li> ';
			})
			menulist += '</ul>';
			$('#allowed_operator').accordion('add', {
				title : n.menuname,
				content : menulist
			});
			$('.easyui-accordion li a').click(function() {
				var tabTitle = $(this).children('.nav').text();
				var url = $(this).attr("rel");
				addTab(tabTitle, url);
				$('.easyui-accordion li div').removeClass("selected");
				$(this).parent().addClass("selected");
			});
		});
	});
});
var _menus = {
	"menus" : [
     {
		"menuname" : "常用功能",
		"menus" : 
		[
		{"menuname" : "发布文章","url" : "article_add.php"},
		{"menuname" : "修改个人信息","url" : "admin_modify_byself.php"}
		]
	 }, 
	 {
		"menuname" : "文章管理",
		"menus" : 
		[
		{"menuname" : "发布文章","url" : "article_add.php"}, 
		{"menuname" : "查看现有文章","url" : "article_show.php"}
		]
	},
	 {
		"menuname" : "栏目管理",
		"menus" : 
		[
		{"menuname" : "添加栏目","url" : "channel_add.php"}, 
		{"menuname" : "查看现有栏目","url" : "channel_show.php"}
		]
	},
	 {
		"menuname" : "单位概况",
		"menus" : 
		[
		{"menuname" : "领导班子","url" : "work_climate_set.php?type=leadership"}, 
		{"menuname" : "内设机构","url" : "work_climate_set.php?type=organization"},
		{"menuname" : "单位简介","url" : "work_climate_set.php?type=profile"},
		{"menuname" : "办公室电话","url" : "work_climate_set.php?type=tel"},
		{"menuname" : "人员招聘","url" : "work_climate_set.php?type=recruit"}
		]
	},
	 {
		"menuname" : "汉阴气候",
		"menus" : 
		[
		{"menuname" : "特色作物","url" : "work_climate_set.php?type=special"}, 
		{"menuname" : "气候特点","url" : "work_climate_set.php?type=climate"},
		{"menuname" : "农作物分布","url" : "work_climate_set.php?type=crops"}
		]
	},
	 {
		"menuname" : "留言管理",
		"menus" : 
		[
		{"menuname" : "留言管理","url" : "guest_book_admin.php"} 
		]
	},
	 {
		"menuname" : "系统管理",
		"menus" : 
		[
		{"menuname" : "更新索引","url" : "search_index_rebuild.php"}, 
		{"menuname" : "系统设置","url" : "sys_information_set.php"},
		{"menuname" : "附件管理","url" : "attachment_manage.php"}
		]
	}
	]

};
//初始化左侧
function InitLeftMenu() {
	$("#allowed_operator").accordion({
		animate : false
	});
	var n = _menus.menus[0];
	var menulist = '';
	menulist += '<ul>';
	$.each(n.menus, function(j, o) {
		menulist += '<li><div><a  href="#" rel="' + o.url + '" ><span>&nbsp;</span><span class="nav">' + o.menuname + '</span></a></div></li> ';
	})
	menulist += '</ul>';

	$('#allowed_operator').accordion('add', {
		title : n.menuname,
		content : menulist
	});

	$('.easyui-accordion li a').click(function() {
		var tabTitle = $(this).children('.nav').text();

		var url = $(this).attr("rel");

		addTab(tabTitle, url);
		$('.easyui-accordion li div').removeClass("selected");
		$(this).parent().addClass("selected");
	});

	//选中第一个
	var panels = $('#allowed_operator').accordion('panels');
	var t = panels[0].panel('options').title;
	$('#nav').accordion('select', t);
}

function addTab(subtitle, url) {
	if (!$('#bottom_navigation').tabs('exists', subtitle)) {
		$('#bottom_navigation').tabs('add', {
			title : subtitle,
			content : createFrame(url),
			closable : true,
		});
	} else {
		$('#bottom_navigation').tabs('select', subtitle);
		$('#mm-tabupdate').click();
	}
	tabClose();
}

function createFrame(url) {
	var s = '<iframe class="operator_iframe" src=' + url + ' scrolling="yes" frameborder="0" width="100%" height="100%" onload="resize();"></iframe>';
	return s;
}
function tabClose() {
	/*双击关闭TAB选项卡*/
	$(".tabs-inner").dblclick(function() {
		var subtitle = $(this).children(".tabs-closable").text();
		$('#bottom_navigation').tabs('close', subtitle);
	})
	/*为选项卡绑定右键*/
	$(".tabs-inner").bind('contextmenu', function(e) {
		$('#mm').menu('show', {
			left : e.pageX,
			top : e.pageY
		});

		var subtitle = $(this).children(".tabs-closable").text();

		$('#mm').data("currtab", subtitle);
		$('#bottom_navigation').tabs('select', subtitle);
		return false;
	});
}

//绑定右键菜单事件
function tabCloseEven() {
	//刷新
	$('#mm-tabupdate').click(function() {
		var currTab = $('#bottom_navigation').tabs('getSelected');
		var url = $(currTab.panel('options').content).attr('src');
		$('#bottom_navigation').tabs('update', {
			tab : currTab,
			options : {
				content : createFrame(url)
			}
		});
	})
	//关闭当前
	$('#mm-tabclose').click(function() {
		var currtab_title = $('#mm').data("currtab");
		$('#bottom_navigation').tabs('close', currtab_title);
	})
	//全部关闭
	$('#mm-tabcloseall').click(function() {
		$('.tabs-inner span').each(function(i, n) {
			var t = $(n).text();
			$('#bottom_navigation').tabs('close', t);
		});
	});
	//关闭除当前之外的TAB
	$('#mm-tabcloseother').click(function() {
		$('#mm-tabcloseright').click();
		$('#mm-tabcloseleft').click();
	});
	//关闭当前右侧的TAB
	$('#mm-tabcloseright').click(function() {
		var nextall = $('.tabs-selected').nextAll();
		if (nextall.length == 0) {
			return false;
		}
		nextall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			$('#bottom_navigation').tabs('close', t);
		});
		return false;
	});
	//关闭当前左侧的TAB
	$('#mm-tabcloseleft').click(function() {
		var prevall = $('.tabs-selected').prevAll();
		if (prevall.length == 0) {
			return false;
		}
		prevall.each(function(i, n) {
			var t = $('a:eq(0) span', $(n)).text();
			$('#bottom_navigation').tabs('close', t);
		});
		return false;
	});

	//退出
	$("#mm-exit").click(function() {
		$('#mm').menu('hide');
	})
}

function resize() {
	$(".operator_iframe").height(this.document.body.clientHeight - 80);
}
